
**********Data are subsetted from 2010 Common Content Survey and UC Davis Election Study*********
**********which was a survey of district "expert informants" who reported on the*******'*********
**********ideological positions of candidates running in their district, among other*************
**********qualities, characteristics, and activities in the 2010 House elections.****************
**********The full UCD Election Study data are available on the project website: ****************
**********https://electionstudy.ucdavis.edu/ ****************************************************

use "CC2010_subset", clear
keep if nochall10==0
g w=v101
*Voting choice for House candidate (1= voted Republican)
recode cc412 (1=0)(2=1)(3/10=.), g(hvote)

*Convert to general coding rule which is Democrats and liberals coded negative; 
*independents and moderates coded 0; Republicans and conservatives coded positive
recode v212d (1=-3)(2=-2)(3=-1)(4=0)(5=1)(6=2)(7=3)(8=.), g(partyid)
recode partyid (-3/-1=-1 "Democrat")(0=0 "Independent")(1/3=1 "Republican"), g(tripid)
recode cc334a (1=-3)(2=-2)(3=-1)(4=0)(5=1)(6=2)(7=3)(8=.), g(libcon)
recode cc334j (1=-3)(2=-2)(3=-1)(4=0)(5=1)(6=2)(7=3)(8=.), g(lcdemcanperceived)
recode cc334k (1=-3)(2=-2)(3=-1)(4=0)(5=1)(6=2)(7=3)(8=.), g(lcrepcanperceived)
replace dlc_pc10=dlc_pc10-4
replace rlc_pc10=rlc_pc10-4

*linear distance measures are based on self-placement on libcon and 
*informant-placed candidate positions (the "pc10" extender indicates 
*variable is "purged" of partisan bias and "combined" aggregated score by
*delegate and screened YouGov panelists) (see Maestas et al 2014).  

*Measure distance from constituent to candidates in district, where candidate positions are
*determined by informant placements.  
gen proxdem=abs(libcon-dlc_pc10)
gen proxrep=abs(libcon-rlc_pc10)

*Measures distance from constituent to candidates in district, where candidate positions 
*are determined by perceptions of constituents (used in Figure 5).
gen proxdemperceived=abs(libcon-lcdemcanperceived)
gen proxrepperceived=abs(libcon-lcrepcanperceived)  

*converts party-based measures to indicate whether the candidate is in the same or opposing party
*as constituent.
gen inpartyprox=proxdem if tripid==-1
replace inpartyprox=proxrep if tripid==1
replace inpartyprox=. if tripid==0
gen outpartyprox=proxdem if tripid==1
replace outpartyprox=proxrep if tripid==-1
replace outpartyprox=. if tripid==0

gen inpartyproxperceived=proxdemperceived if tripid==-1
replace inpartyproxperceived=proxrepperceived if tripid==1
replace inpartyproxperceived=. if tripid==0
gen outpartyproxperceived=proxdemperceived if tripid==1
replace outpartyproxperceived=proxrepperceived if tripid==-1
replace outpartyproxperceived=. if tripid==0

gen inpartyadv=outpartyprox-inpartyprox
gen incumbinparty=1 if tripid==-1&deminc10==1
replace incumbinparty=1 if tripid==1&repinc10==1
replace incumbinparty=-1 if tripid==-1&repinc10==1
replace incumbinparty=-1 if tripid==1&deminc10==1
replace incumbinparty=0 if open10==1
replace incumbinparty=. if tripid==0

gen votedinparty=1 if hvote==1&tripid==1
replace votedinparty=1 if hvote==0&tripid==-1
replace votedinparty=0 if hvote==0&tripid==1
replace votedinparty=0 if hvote==1&tripid==-1

*disrict presidential vote for Figure 4.
egen dpresvote=rowmean(dpres08 dpresvote04)
gen incpres=dpresvote if pty111==1
replace incpres=100-dpresvote if pty111==2
gen rpresvote=100-dpresvote


gen incumbency=1 if repinc10==1
replace incumbency=-1 if deminc10==1
replace incumbency=0 if open10==1


*Table 1outpartyprox
logit votedinparty inpartyadv  incumbinparty sample06 [pw=w], cl(district)

*Table 2
logit votedinparty  inpartyprox outpartyprox incumbinparty sample06 [pw=w], cl(district)

*Figure 1
*Effect of distance from other party
margins ,at(outpartyprox=(0(.5)5) incumbinparty=(0) inpartyprox=(1.0))
margins ,at(outpartyprox=(0(.5)5) incumb=(0) inpartyprox=(1.0))
marginsplot, 	///
	recast(line) plotopts(lcolor(black)) recastci(rarea) ciopts(fcolor(gs14) lcolor(white)) 	///
	ytitle(Probability of voting for own party candidate) ytitle(, margin(medsmall)) 	///
	ylabel(.5(.1)1, angle(horizontal) format(%3.1f) nogrid) 	///
	xtitle(Distance from other party's candidate) xtitle(, margin(medsmall)) 	///
	xlabel(0(1)5) title(" ") graphregion(fcolor(white) lcolor(black))
*Effect of distance from own party
margins ,at(inpartyprox=(0(.5)5) incumbinparty=(0))
marginsplot, 	///
	recast(line) plotopts(lcolor(black)) recastci(rarea) ciopts(fcolor(gs14) lcolor(white)) 	///
	ytitle(Probability of voting for own party candidate) ytitle(, margin(medsmall)) 	///
	ylabel(.5(.1)1, angle(horizontal) format(%3.1f) nogrid) 	///
	xtitle(Distance from own party's candidate) xtitle(, margin(medsmall)) 	///
	xlabel(0(1)5) title(" ") graphregion(fcolor(white) lcolor(black))
	
*Figure 5
twoway ///
     (lpolyci inpartyproxperceived inpartyprox [aweight = w], bwidth(.75) lcolor(black) clwidth(medthick) clpattern(solid) fcolor(gs12) alcolor(gs12)) ///
     (lpolyci outpartyproxperceived outpartyprox [aweight = w], bwidth(.75) lcolor(black) clwidth(medthick) clpattern(shortdash) fcolor(gs12) alcolor(gs12)) ///
     , ytitle(Perceived ideological distance from candidate) ///
     ytitle(, size(small) margin(medsmall)) ylabel(0(1)6, labsize(small) angle(horizontal) glcolor(gs14) gmin gmax) ///
     xtitle(Actual ideological distance from candidate) xtitle(, size(small) margin(medsmall)) ///
     xlabel(0(1)5, labsize(small)) legend(size(small)) xsize(5) ysize(5) graphregion(fcolor(white) lcolor(black))


